package com.pfzy.utildemo.activity.record.recording.permission;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;

import com.pfzy.lib.sdk.permission.UXPermission;
import com.pfzy.lib.sdk.permission.callback.PermissionCallBack;
import com.pfzy.utildemo.activity.record.recording.UXDRManager;
import com.pfzy.utildemo.activity.record.recording.callback.DialogCallBack;
import com.pfzy.utildemo.activity.record.recording.callback.UXDRDialogCallBack;
import com.pfzy.utildemo.activity.record.recording.callback.UXMikeCallBack;
import com.pfzy.utildemo.activity.record.recording.dialog.UXDriverRecordingDialog;

import java.lang.ref.WeakReference;
import java.util.List;

public class UXDRPermission {
    private static WeakReference<Dialog> mWeakReference;

    public static boolean hasePermissionMike(Context context) {
        if (context == null) {
            return false;
        }
        if (Build.VERSION.SDK_INT < 23) {
            return true;
        }
        int permission = context.checkSelfPermission("android.permission.RECORD_AUDIO");
        return permission == 0;
    }

    /**
     * 请求许可Mike
     */
    public static void requestPermissionMike(final Activity context, final UXMikeCallBack uxMikeCallBack) {
        UXPermission.with(context).permission("android.permission.RECORD_AUDIO").request(new PermissionCallBack() {
            @Override
            public void hasPermission(List<String> granted, boolean isAll) {
                if (uxMikeCallBack != null) {
                    uxMikeCallBack.havePermission();
                }
            }

            @Override
            public void noPermission(List<String> denied, boolean quick) {
                super.noPermission(denied, quick);
                UXDRDialogCallBack uxDialogCallBack = UXDRManager.getInstance().getRecodingParams().getDialogCallBack();
                DialogCallBack dialogCallBack1 = new DialogCallBack() {
                    @Override
                    public void cancel() {
                        if (uxMikeCallBack != null) {
                            uxMikeCallBack.noPermission();
                        }
                    }

                    @Override
                    public void confirm() {
                        if (uxMikeCallBack != null) {
                            uxMikeCallBack.noPermission2setting();
                        }
                        UXPermission.gotoDefaultPermissionSettings(context);
                    }
                };
                Dialog weakDialog = null;
                if (UXDRPermission.mWeakReference != null) {
                    weakDialog = UXDRPermission.mWeakReference.get();
                }
                if (weakDialog != null) {
                    weakDialog.dismiss();
                    UXDRPermission.mWeakReference = null;
                }
                Dialog dialog = uxDialogCallBack == null ? new UXDriverRecordingDialog(context, dialogCallBack1) : uxDialogCallBack.getDialog(context, dialogCallBack1);
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog2) {
                        if (UXDRPermission.mWeakReference != null && UXDRPermission.mWeakReference.get() != null) {
                            Dialog dialog1 = UXDRPermission.mWeakReference.get();
                            if (!dialog1.isShowing()) {
                                UXDRPermission.mWeakReference = null;
                            }
                        }
                    }
                });
                UXDRPermission.mWeakReference = new WeakReference(dialog);
                dialog.show();
            }

            @Override
            public void needCheckPermission() {
                super.needCheckPermission();
                if (uxMikeCallBack != null) {
                    uxMikeCallBack.havePermission();
                }
            }
        });
    }
}
